package ciyuan.Effect;

import ciyuan.Constants.PlayerDataType;
import ciyuan.Damage.MyDamageSource;
import ciyuan.DataManager.PlayerDataManager;
import ciyuan.Tasks.PlayerTaskManager;
import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.potion.EffectInstance;
import net.minecraftforge.event.entity.living.LivingEntityUseItemEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

import java.util.Calendar;
import java.util.Random;

/**
 * @author 150149
 */
@Mod.EventBusSubscriber
public class EffectResult {

    private static final String FoodTimeString = "ciyuanrpgfoodtime";

    @SubscribeEvent
    public static void onBreak(PlayerEvent.BreakSpeed event) {
        if (event.getPlayer().hasEffect(Effects.dislocation)) {
            event.setNewSpeed(0.5f);
        }
    }

    @SubscribeEvent
    public static void onEat(LivingEntityUseItemEvent.Finish event) {
        if (event.getItem().getTag() != null) {
            CompoundNBT compoundNBT = event.getItem().getTag();
            if (compoundNBT.contains(FoodTimeString)) {
                long foodTime = compoundNBT.getLong(FoodTimeString);
                if (event.getEntity() instanceof PlayerEntity) {
                    PlayerEntity playerEntity = (PlayerEntity) event.getEntity();
                    if (playerEntity.getCommandSenderWorld().getGameTime() - foodTime > 70000) {
                        playerEntity.addEffect(new EffectInstance(Effects.food_poisoning, 3000, 0));
                    }
                }
            }
        }
    }

    static {
        PlayerTaskManager.register(playerEntity -> {
            Random random = new Random(Calendar.getInstance().getTimeInMillis());

            if (playerEntity.hasEffect(Effects.food_poisoning)) {
                if (random.nextInt(100) < 10) {
                    playerEntity.addEffect(new EffectInstance(net.minecraft.potion.Effects.CONFUSION, 140, 0, false, false, false));
                }
                if (random.nextInt(100) < 30) {
                    playerEntity.addEffect(new EffectInstance(net.minecraft.potion.Effects.HUNGER, 120, 0, false, false, false));
                }
            }

            if (playerEntity.hasEffect(Effects.sprain_injury)) {
                if (random.nextInt(100) < 30) {
                    playerEntity.addEffect(new EffectInstance(net.minecraft.potion.Effects.JUMP, 60, -5, false, false, false));
                }
                if (random.nextInt(100) < 10) {
                    playerEntity.addEffect(new EffectInstance(net.minecraft.potion.Effects.MOVEMENT_SLOWDOWN, 60, 2, false, false, false));
                }
            }

            if (playerEntity.hasEffect(Effects.internal_injury)) {
                playerEntity.hurt(MyDamageSource.bleedDamage, playerEntity.getMaxHealth() / 20);
            }

            if (playerEntity.hasEffect(Effects.frostbite)) {
                playerEntity.addEffect(new EffectInstance(net.minecraft.potion.Effects.MOVEMENT_SLOWDOWN, 20, 2, false, false, false));
                if (random.nextInt(100) < 40) {
                    playerEntity.hurt(MyDamageSource.bleedDamage, playerEntity.getMaxHealth() / 20);
                }
            }

            if (playerEntity.hasEffect(Effects.fever)) {
                playerEntity.addEffect(new EffectInstance(net.minecraft.potion.Effects.WEAKNESS, 30, 2, false, false, false));
                playerEntity.addEffect(new EffectInstance(Effects.thirsty, 40, 0, false, false, false));
            }

            if (playerEntity.hasEffect(Effects.weakness_analysis)) {
                playerEntity.addEffect(new EffectInstance(net.minecraft.potion.Effects.MOVEMENT_SPEED, 20, 1, false, false, false));
            }

            if (playerEntity.hasEffect(Effects.jungle_phantom)) {
                playerEntity.addEffect(new EffectInstance(net.minecraft.potion.Effects.JUMP, 20, 2, false, false, false));
            }

            if (playerEntity.hasEffect(Effects.heatstroke)) {
                playerEntity.addEffect(new EffectInstance(Effects.thirsty, 40, 0, false, false, false));
                if (random.nextInt(100) < 40) {
                    playerEntity.hurt(MyDamageSource.bleedDamage, playerEntity.getMaxHealth() / 20);
                }
            }

            double temperature = PlayerDataManager.getDouble(playerEntity, PlayerDataType.TEMPERATURE);
            if (temperature < 12) {
                playerEntity.addEffect(new EffectInstance(Effects.frostbite, 40, 0));
            }
            if (temperature > 60) {
                playerEntity.addEffect(new EffectInstance(Effects.heatstroke, 40, 0));
            }

            Block block = playerEntity.level.getBlockState(playerEntity.blockPosition().below()).getBlock();
            if (block.is(Blocks.SNOW) || block.is(Blocks.SNOW_BLOCK) || block.getDescriptionId().toLowerCase().contains("ice") || block.is(Blocks.WATER) || block.getDescriptionId().toLowerCase().contains("snow")) {
                PlayerDataManager.setServerPlayerData(playerEntity, PlayerDataType.TEMPERATURE, temperature - 0.3);
            } else if (block.isBurning(playerEntity.getFeetBlockState(), playerEntity.level, playerEntity.blockPosition().below())) {
                PlayerDataManager.setServerPlayerData(playerEntity, PlayerDataType.TEMPERATURE, temperature + 1);
            } else if (playerEntity.getFeetBlockState().getBlock().getDescriptionId().toLowerCase().contains("fire")) {
                PlayerDataManager.setServerPlayerData(playerEntity, PlayerDataType.TEMPERATURE, temperature + 3);
            } else if (playerEntity.getFeetBlockState().getBlock().is(Blocks.LAVA)) {
                PlayerDataManager.setServerPlayerData(playerEntity, PlayerDataType.TEMPERATURE, temperature + 10);
            } else if (playerEntity.getFeetBlockState().getBlock().is(Blocks.WATER)) {
                PlayerDataManager.setServerPlayerData(playerEntity, PlayerDataType.TEMPERATURE, temperature - 0.5);
            }

        });
    }
}
